<?php
class ListingApp extends MemberbaseApp
{
	var $_listing_mod;
	
	function __construct(){
		$this->ListingApp();
	}
	
	function ListingApp(){
		parent::__construct();
		$this->_listing_mod=&m('listing');
	}
	
	function index(){
		$page=$this->_get_page();
		$listing=$this->_listing_mod->find(array(
				'join'=>'belong_to_brand',
				'order'=>'listing_time desc',
				'limit'=>$page['limit']	,
				'count'=>true
		));
		$page['item_count'] = $this->_listing_mod->getCount();
		$this->_format_page($page);
		$this->assign('page_info', $page);
		$this->assign('listing',$listing);
		$this->display('listing.index.html');
	}
	
	function edit(){
		$l_id=isset($_GET['l_id'])?intval($_GET['l_id']):0;
		if (!$l_id){
			$this->show_warning('listing_no_exist');
			return;
		}
		$file_mod =& m('upload');
		if (!IS_POST){
			$brand_mod=&m('brand');
			$brand_list=$brand_mod->find(array(
					'join'=>'belong_company',
					'conditions'=>'brand.if_show=1',
					'order'=>'brand.brand_id desc'
			));
			$this->assign('brand_list',$brand_list);
			$listing=$this->_listing_mod->get($l_id);
			if (empty($listing)){
				$this->show_warning('listing_no_exist');
				return;
			}
			$listing_file=$file_mod->find(array(
					'conditions' => 'file_type = ' . BELONGS_LISTING_IMG . ' AND key_id = ' . $l_id,
					'order' => 'file_id ASC',
			));
			$this->assign('listing',$listing);
			$this->assign('listing_file',$listing_file);
			$this->assign('belongs_listing_img',BELONGS_LISTING_IMG);
			$this->assign('default_cust_id',DEFAULT_CUSTOMER_KEY_ID);
			$this->display('listing.form.html');
		}else{
			$data=array();
			$listing_code=trim($_POST['listing_code']);
			if (!$listing_code){
				$this->show_warning('listing_code_empty');
				return;
			}
			$data['listing_code']=$listing_code;
			$data['brand_id']=isset($_POST['brand_id'])?intval($_POST['brand_id']):0;
			$this->_listing_mod->edit($l_id,$data);
			if ($this->_listing_mod->has_error()){
				$this->show_warning($this->_listing_mod->get_error());
				return;
			}
			$this->show_message('edit_listing_success',Lang::get('listing_manage'),'index.php?app=listing');
		}
	}
	
	
	function add(){
		if (!IS_POST){
			$brand_mod=&m('brand');
			$brand_list=$brand_mod->find(array(
					'join'=>'belong_company',
					'conditions'=>'brand.if_show=1',
					'order'=>'brand.brand_id desc'
			));
			$this->assign('brand_list',$brand_list);
			$this->assign('belongs_listing_img',BELONGS_LISTING_IMG);
			$this->assign('default_cust_id',DEFAULT_CUSTOMER_KEY_ID);
			$this->display('listing.form.html');
		}else{
			$data=array();
			$listing_code=trim($_POST['listing_code']);
			if (!$listing_code){
				$this->show_warning('listing_code_empty');
				return;
			}
			$data['listing_code']=$listing_code;
			$data['brand_id']=isset($_POST['brand_id'])?intval($_POST['brand_id']):0;
			$data['listing_time']=gmtime();
			$list_id=$this->_listing_mod->add($data);
			if ($this->_listing_mod->has_error()){
				$this->show_warning($this->_listing_mod->get_error());
				return;
			}
			$listing_img_id=isset($_POST['listing_img_id'])?$_POST['listing_img_id']:'';
			$file_mod =& m('upload');
			if(!empty($listing_img_id))
			{
				$file_ids=implode(",",$listing_img_id);
				$cust_files_info=$file_mod->find(array(
						"conditions"=>"file_id in({$file_ids}) AND file_type=".BELONGS_LISTING_IMG." AND key_id=".DEFAULT_CUSTOMER_KEY_ID." and session_id='".session_id()."'"
								));
					
				foreach($cust_files_info as $item)
				{
					$file_path = str_replace(DEFAULT_CUSTOMER_KEY_ID, $list_id, $item['file_path']);
					if(!file_exists(dirname($file_path))) {
						mkdir(dirname($file_path), 0777);
					}
					rename($item['file_path'], $file_path);
					$file_mod->edit("file_id={$item['file_id']}",array(
							"file_path"=>$file_path,
							"key_id"=>$list_id,
							"session_id"=>session_id()
					));
				}
			}
			$this->show_message('add_listing_success',Lang::get('listing_manage'),'index.php?app=listing');
		}
	}
	
	
	function drop(){
		$l_id=isset($_GET['l_id'])?intval($_GET['l_id']):0;
		if (!$l_id){
			$this->show_warning('listing_no_exist');
			return;
		}
		$this->_listing_mod->drop($l_id);
		if ($this->_listing_mod->has_error()){
			$this->show_warning($this->_listing_mod->get_error());
			return;
		}
		$this->show_message('drop_success');
	}
}